//
//  SPLiveListViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/22.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPLiveListViewController.h"
#import "SPCommon.h"
#import "SPLiveTableViewCell.h"
#import "SPLiveStatusTableViewController.h"
#import "SPLiveListModel.h"

@interface SPLiveListViewController ()
@property (weak, nonatomic) IBOutlet UILabel *title_jjkb;
@property (weak, nonatomic) IBOutlet UILabel *title_zzzb;
@property (weak, nonatomic) IBOutlet UILabel *line_jjkb;
@property (weak, nonatomic) IBOutlet UILabel *line_zzzb;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) LiveType type;
@property (nonatomic,strong) NSMutableArray<SPLiveListModel *> *list_jjkb;
@property (nonatomic,strong) NSMutableArray<SPLiveListModel *> *list_zzzb;

@end

@implementation SPLiveListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"直播列表";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerV = [UIView new];
    headerV.backgroundColor = RZ_COLOR(245, 245, 245);
    headerV.height = 15;
    self.tableView.tableHeaderView = headerV;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPLiveTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPLiveTableViewCell class])];
    
    [self jjkb_onClick];
    [self fetchData];
}




#pragma mark--

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if(_type == LiveType_JJKB){
        count = self.list_jjkb.count;
    }else{
        count = self.list_zzzb.count;
    }
    
    return count;;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPLiveTableViewCell *liveCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPLiveTableViewCell class])];
    liveCell.type = _type;
    return liveCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPLiveStatusTableViewController *status = [[SPLiveStatusTableViewController alloc] initWithLiveType:_type];
    [self.navigationController pushViewController:status animated:YES];
}



- (IBAction)jjkb_onClick {
    
    _type = LiveType_JJKB;
    [self setupSelectedWithLiveType:_type];
    [self.tableView reloadData];
}


- (IBAction)zzzb_onClick {
    
    _type = LiveType_ZZZB;
    [self setupSelectedWithLiveType:_type];
    [self.tableView reloadData];
    
}


- (void)setupSelectedWithLiveType:(LiveType)type
{
    [self setupTitle:_title_jjkb line:_line_jjkb selected:type == LiveType_JJKB];
    [self setupTitle:_title_zzzb line:_line_zzzb selected:type == LiveType_ZZZB];
}

- (void)setupTitle:(UILabel *)title line:(UILabel *)line selected:(BOOL)selected
{
    title.font = [UIFont fontWithName:selected?@"PingFangSC-Semibold":@"PingFang-SC-Medium" size:15];
    line.hidden = !selected;
}


#pragma mark--fetchData
- (void)fetchData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=dai_live&token=568e86c8630f7b44f29a23ba211d9f25
     参数 page 第几页 psize 每页多少条 默认5
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"dai_live";
//    dict[@"page"] = @(1);
//    dict[@"psize"] = @(5);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            
            NSArray<SPLiveListModel *> *items = [SPLiveListModel mj_objectArrayWithKeyValuesArray:res[@"data"][@"acts"]];
            self.list_jjkb = [NSMutableArray array];
            self.list_zzzb = [NSMutableArray array];
            [items enumerateObjectsUsingBlock:^(SPLiveListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
            }];
            [self.tableView reloadData];
            
            
        }else{
            [WToast showWithText:res[@"resultMessage"]];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}

@end
